//
//  XDStatusCell.swift
//  weibo
//
//  Created by lucy on 2016/10/26.
//  Copyright © 2016年 com.huashengHuaSheng. All rights reserved.
//

import UIKit

class XDStatusCell: UITableViewCell {
    
    var bottomViewTopCons: Constraint?

    var statusViewModel: XDStatusViewModel?{
        didSet{
            //// 给原创微博赋值
            originalView.statusViewModel = statusViewModel
            // 卸载约束
            bottomViewTopCons?.uninstall()
            //没有转发微博
            if statusViewModel?.statusModel?.retweeted_status == nil {
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
                // 隐藏
                retweetView.isHidden = true
                
            }else {
                //// 切记先赋值
                retweetView.statusViewModel = statusViewModel
                
                // 有转发微博
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                // 显示
                retweetView.isHidden = false
            }
            ////底部视图赋值
            bottomView.statusViewModel = statusViewModel


        }

    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK-设置视图
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
    
        originalView.snp_makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
            //            make.leading.right.top.equalTo(contentView)
            // 此约束已经放到原创微博中
            //            make.height.equalTo(50)
        }
        
        retweetView.snp_makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
            // 该约束已经放到了转发微博中
            //            make.height.equalTo(50)
        }
        
        bottomView.snp_makeConstraints { (make) in
            self.bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(35)
            make.bottom.equalTo(contentView)
        }
        
    }
    
    // MARK: - 懒加载控件
    // 原创微博
    private lazy var originalView: XDStatusOriginalView = XDStatusOriginalView()
    // 转发微博
    private lazy var retweetView: XDStatusRetweenView = XDStatusRetweenView()
    // 底部视图
    private lazy var bottomView: XDStatusBottomView = XDStatusBottomView()
    
}






















